package com.henu.ocr.controller;

import com.henu.ocr.entity.Template;
import com.henu.ocr.entity.TemplateItem;
import com.henu.ocr.service.TemplateService;
import com.henu.ocr.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Tag(name = "模板管理接口")
@RequestMapping("/templates")
public class TemplateController {

    @Resource
    private TemplateService templateService;

    public static class TemplateCreateRequest {
        private Template template;
        private List<TemplateItem> items;
        public Template getTemplate() { return template; }
        public void setTemplate(Template template) { this.template = template; }
        public List<TemplateItem> getItems() { return items; }
        public void setItems(List<TemplateItem> items) { this.items = items; }
    }

    @Operation(summary = "创建模板（含初始项）")
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Result<?> createTemplate(@RequestBody TemplateCreateRequest request,
                                    @RequestHeader("token") String token) {
        try {
            templateService.createTemplate(request.getTemplate(), request.getItems(), token);
            return Result.OK("创建成功");
        } catch (IllegalArgumentException e) {
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            return Result.Exception("服务器异常");
        }
    }

    @Operation(summary = "删除模板（级联删除所有项）")
    @DeleteMapping("/{templateId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Result<?> deleteTemplate(@PathVariable Integer templateId,
                                    @RequestHeader("token") String token) {
        try {
            templateService.deleteTemplate(templateId, token);
            return Result.OK(null);
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "更新模板基本信息")
    @PutMapping("/{templateId}")
    public Result<?> updateTemplate(@PathVariable Integer templateId,
                                    @RequestBody Template template,
                                    @RequestHeader("token") String token) {
        try {
            template.setTemplateId(templateId);
            templateService.updateTemplate(template, token);
            return Result.OK("更新成功");
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "获取所有模板")
    @GetMapping
    public Result<List<Template>> getAllTemplates(@RequestHeader("token") String token) {
        try {
            List<Template> data = templateService.getAllTemplates(token);
            return Result.OK(data.isEmpty() ? "暂无数据" : "查询成功", data);
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "获取模板详情（含所有项）")
    @GetMapping("/{templateId}")
    public Result<Template> getTemplateDetail(@PathVariable Integer templateId, @RequestHeader("token") String token) {
        try {
            Template data = templateService.getTemplateWithItems(templateId, token);
            return data != null ?
                    Result.OK("获取成功", data) :
                    Result.error(404, "模板不存在");
        } catch (IllegalArgumentException e) {
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "为模板添加新项")
    @PostMapping("/{templateId}/items")
    public Result<?> addItemsToTemplate(@PathVariable Integer templateId,
                                        @RequestBody List<TemplateItem> items,
                                        @RequestHeader("token") String token) {
        try {
            templateService.addItemsToTemplate(templateId, items, token);
            return Result.OK("添加成功");
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "删除模板中的单个项")
    @DeleteMapping("/{templateId}/items/{itemId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Result<?> deleteTemplateItem(@PathVariable Integer templateId,
                                        @PathVariable Integer itemId,
                                        @RequestHeader("token") String token) {
        try {
            templateService.deleteTemplateItem(templateId, itemId, token);
            return Result.OK(null);
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }
}